//2009/08/20 20:35:08
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <sstream>
#include <algorithm>

using namespace std;

#define	 MAX 	2500

class MooingCows
{
public:
    int dissatisfaction(vector <string> farmland)
    {
        int mmin = -1;
		int idx = 0;
		memset(x,0,sizeof(x));
		memset(y,0,sizeof(y));
		//int idy = 0;
        for (int i=0; i<farmland.size(); i++)
            for (int j=0; j<farmland[i].size(); j++)
				if(farmland[i][j] == 'C')
				{
					x[idx] = i;
					y[idx] = j;
					idx++;
				}
		for(int i=0; i<idx; i++)
		{
			int total = 0;
			for(int j=0; j<idx; j++)
					total += (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]);
			if(total < mmin || mmin == -1)
				mmin = total;
		}
		return mmin;
    }
private:
	int x[MAX];
	int y[MAX];
};
